<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class ControlItemBase {
	var $feObj = NULL;
	var $modelObj = NULL;
	var $vObj = NULL;
	
	var $actionHandler = NULL;
	var $displayHandler = NULL;
	
	//contructor
	function ControlItemBase($config = '') {
		if( is_array($config) ) {
			foreach($config as $k => $v) {
				$this->$k = $v;
			}
		}
		
		//init internal
		$this->_init();
	}
	
	function _init() {
		//model
		if( $this->class_file != '' && file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $this->class_file) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $this->class_file);
		}
		$this->modelObj = new $this->class_name();
		$this->modelObj->format = true;
		$this->modelObj->format_check = true;
		if( isset($this->class_param) && is_array($this->class_param) ) {
			foreach($this->class_param as $k => $v) {
				$this->modelObj->$k = $v;
			}
		}
		
		//view
		global $feObj;
		$this->feObj = $feObj;
		
		//validate
		//$this->vObj = new CommonValidate();
		global $vObj;
		$this->vObj = $vObj;
		
		//trigger
		$this->_init_trigger();
		
		//inject
		$this->_init_inject();
		
		//filter
		$this->_init_filter();
	}
	
	function _init_trigger() {
		if( isset($this->triggers) && is_array($this->triggers) ) {
			foreach($this->triggers as $k => $v) {
				if( $k == 'model' && is_array($v) ) {
					$this->modelObj->triggers = $v;
				}
			}
		}
	}
	
	function _init_inject() {
		if( isset($this->inject) && is_array($this->inject) ) {
			foreach($this->inject as $k => $v) {
				if( is_array($v) && (!isset($v['status']) || $v['status'] == '1') ) {
					if( $v['type'] == 'cm' ) {
						if( $v['class_file'] != '' && file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $v['class_file']) ) {
							include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $v['class_file']);
						}
						$cmObj = new $v['class_name']();
						if( isset($v['init']) && is_array($v['init']) ) {
							foreach($v['init'] as $ik => $iv) {
								$cmObj->$ik = $iv;
							}
						}
						//$vs = $cmObj->$v['method']();
						$vs = call_user_func_array(array($cmObj, $v['method']), $v['params']);
						$this->feObj->addInject($k, $vs);
					} elseif( $v['type'] == 'm' ) {
						$vs = call_user_func_array(array($this, $v['method']), $v['params']);
						$this->feObj->addInject($k, $vs);
					} elseif( $v['type'] == 'f' ) {
						$vs = call_user_func_array($v['function'], $v['params']);
						$this->feObj->addInject($k, $vs);
					}
				}
			}
		}
	}
	
	function _init_filter() {
		$this->filter['add'] = array('check' => 'field_image', 'fields' => array('^field_image$'));
		$this->filter['update'] = array('check' => 'field_image', 'fields' => array('^field_image$'));
	}
	
	//init
	function init() {
		
	}
	
	//process
	function process() {
		$this->_doBefore();
		$this->_doAction();
		$this->_doDisplay();
		$this->_doAfter();
	}
	
	//methods
	function _doBefore() {
		
	}
	
	function _doAfter() {
		
	}
	
	function _doAction() {
		if( $this->_check_action('add') ) {
			$this->vObj->check($_POST, array('name'));
			if( $this->vObj->_valid() ) {
				$data_array = $this->_doFilter($_POST, 'add');
				if( $this->check_action_add($data_array) ) {
					$this->modelObj->addItem($data_array);
					//
					$this->_redirect('', array('id','op','action'), 's');
				}
			} else {
				$GLOBALS['_SUBMITTED_'] = 1;
				
				$vm = $this->vObj->_result("Error");
				$this->feObj->addInject('vm', $vm);
			}
		} elseif( $this->_check_action('update') ) {
			$data_array = array();
			$id = trim(strtolower(util_get_param($_POST, 'id')));
			if( $id > 0 ) {
				$this->vObj->check($_POST, array('name'));
				if( $this->vObj->_valid() ) {
					$data_array = $this->_doFilter($_POST, 'update');
					if( $this->check_action_update($id, $data_array) ) {
						$this->modelObj->updateItem($id, $data_array);
						//
						$this->_redirect('', array('id','op','action'), 's');
					}
				} else {
					$GLOBALS['_SUBMITTED_'] = 1;
					
					$vm = $this->vObj->_result("Error");
					$this->feObj->addInject('vm', $vm);
				}
			} else {
				$this->_redirect('', array('id','op','action'), 's');
			}
		} elseif( $this->_check_action('delete') ) {
			$id = trim(strtolower(util_get_param($_POST, 'id')));
			if( $id > 0 ) {
				$this->modelObj->deleteItem($id);
				//
				$this->_redirect('', array('id','op','action'), 's');
			}
		} elseif( $this->_check_action('dodelete') || $this->_check_request('dodelete') ) {
			$id = trim(strtolower(util_get_param($_GET, 'id')));
			if( $id > 0 ) {
				$this->modelObj->deleteItem($id);
				//
				$this->_redirect('', array('id','op','action'), 's');
			}
		} elseif( $this->_check_action('delete%ss') ) {
			$ids = util_get_param($_POST, 'cb');
			if( is_array($ids) ) {
				$this->modelObj->deleteItems($ids);
				//
				$this->_redirect('', array('id','op','action'), 's');
			}
		} elseif( $this->_check_action('toadd') ) {
			$this->_redirect($this->feObj->request_link_current . $this->_make_request('add') . '/');
		} elseif( $this->_checkActionHandler() == true ) {
			$this->_doActionHandler();
		}
	}
	
	function _checkActionHandler() {
		return (!empty($this->actionHandler) ? true : false);
	}
	
	function _doActionHandler() {
		$f = $this->actionHandler;
		if( is_array($f) && sizeof($f) >= 2 ) {
			if( $f['0'] == 'this' ) {
				call_user_func_array(array($this, $f['1']), $this);
			} elseif( is_object($f['0']) ) {
				call_user_func_array(array($f['0'], $f['1']), $this);
			}
		} elseif( method_exists($this, $f) ) {
			$this->{$f}(NULL);
		} elseif( function_exists($f) ) {
			call_user_func_array($f, $this);
		}
		
		return false;
	}
	
	function _doDisplay() {
		$is_default = false;
		
		if( $this->_check_request('toaddcategory') ) {
			//
			$link = $this->feObj->request_link_current;
			if( substr($link, -1) == '/' ) {
				$link = substr($link, 0, -1) . '_category';
			} else {
				$link .= '_category';
			}
			$this->feObj->redirect($link, 's');
		} elseif( $this->_check_request('add') ) {
			$this->feObj->content_wrap['mainpart'] = $this->_make_request('add');
		} elseif( $this->_check_request('view') || $this->_check_request('edit') ) {
			$mainpart = $this->_make_request($this->feObj->operation);
			$id = $this->feObj->_request['id'];
			if( $id > 0 ) {
				$_valid = $this->modelObj->checkItem($id);
				if( $_valid == true ) {
					$item = $this->modelObj->getItem($id, 'id', -1);
					
					$this->feObj->content_wrap['mainpart'] = $mainpart;
					$this->feObj->content_wrap['item'] = $item;
				} else {
					$this->feObj->content_wrap['mainpart'] = $this->_make_request('no');
				}
			} else {
				$is_default = true;
			}
		} elseif( $this->_checkDisplayHandler() == true ) {
			$this->_doDisplayHandler();
		} else {
			$is_default = true;
		}
		
		//
		if( $is_default == true ) {
			$filter = $this->_get_items_filter();
			$limit_length = $this->feObj->limit_length;
			$limit_start = ($this->feObj->limit_page - 1) * $limit_length;
			$limit_total = $this->modelObj->getItemsTotal($filter, -1);
			//
			$this->modelObj->setQueryLimitStart($limit_start);
			$this->modelObj->setQueryLimitLength($limit_length);
			$items = $this->modelObj->getItems($filter, -1, true);
			//
			$parameters = util_get_all_get_params(array('page'));
			$link_array = array(
						'page' => '',
						'parameters' => $parameters,
							);
			$navigation = util_common_navigation($link_array, $limit_total, $limit_start, $limit_length);
			
			//			
			$this->feObj->content_wrap['items'] = $items;
			$this->feObj->content_wrap['navigation'] = $navigation;
		}
		
		//
		echo $this->feObj->display($this->tpl_file);
	}
	
	function _checkDisplayHandler() {
		return (!empty($this->displayHandler) ? true : false);
	}
	
	function _doDisplayHandler() {
		$f = $this->displayHandler;
		if( is_array($f) && sizeof($f) >= 2 ) {
			if( $f['0'] == 'this' ) {
				call_user_func_array(array($this, $f['1']), $this);
			} elseif( is_object($f['0']) ) {
				call_user_func_array(array($f['0'], $f['1']), $this);
			}
		} elseif( method_exists($this, $f) ) {
			$this->{$f}(NULL);
		} elseif( function_exists($f) ) {
			call_user_func_array($f, $this);
		}
		
		return false;
	}
	
	function _doFilter($data_array, $type) {
		if( isset($this->filter[$type]) ) {
			$check = $this->filter[$type]['check'];
			$fields = $this->filter[$type]['fields'];
			if( !is_array($check) ) {
				$check = array($check);
			}
			if( !is_array($fields) ) {
				$fields = array($fields);
			}
			$pass = true;
			foreach($check as $k => $v) {
				if( isset($data_array[$v]) && strlen($data_array[$v]) == 0 ) {
					$pass = false;
				}
			}
			if( $pass == false ) {
				foreach($data_array as $dk => $dv) {
					foreach($fields as $k => $v) {
						if( ereg($v, $dk) == true ) {
							unset($data_array[$dk]);
						}
					}
				}
			}
		}
		
		return $data_array;
	}
	
	//
	function _get_items_filter() {
		$filter = array();
		if( is_array($_GET) ) {
			foreach($_GET as $k => $v) {
				if( in_array($k, array('op', 'action')) ) {
					//
				} elseif( strstr($k, 'id') !== false && $v === '' ) {
					//
				} elseif( $k == 'keyword' ) {
					$filter[$k] = $v;
				} else {
					$filter[$k . ':='] = $v;
				}
			}
		}
		
		return $filter;
	}
	
	//
	function _check_action($action) {
		if( in_array($this->feObj->action, array($action, $action . $this->cname, sprintf($action, $this->cname))) ) {
			return true;
		}
		
		return false;
	}
	
	function _make_action($action) {
		return $action . $this->cname;
	}
	
	function _check_request($request) {
		$_r = $this->feObj->operation;
		if( in_array($_r, array($request, $request . $this->cname, sprintf($request, $this->cname))) ) {
			return true;
		}
		
		return false;
	}
	
	function _make_request($request) {
		return $request . $this->cname;
	}
	
	function _redirect($url = '', $ps = '', $msg = '') {
		if( $url == '' ) {
			$url = $this->feObj->request_link_current;
		}
		if( !is_array($ps) ) {
			$ps = array();
		}
		$psd = $psa = array();
		$psd[] = 'msg';
		foreach($ps as $k => $v) {
			if( !is_numeric($k) ) {
				$psa[] = $k .'=' . $v;
			} else {
				$psd[] = $v;
			}
		}
		$pss = util_get_all_get_params($psd);
		if( substr($pss, -1) == '&' ) {
			$pss = substr($pss, 0, -1);
		}
		if( is_array($psa) ) {
			$pss .= implode('&', $psa);
		}
		if( $msg == '' ) {
			$msg = 's';
		}
		$this->feObj->redirect($url . '?' . $pss, $msg);
	}
	
	//DISPLAY
	//
	function display_default() {
		
	}
	
	function display_item() {
		
	}
	
	function display_item_detail() {
		
	}
	
	function display_item_add() {
		$this->feObj->content_wrap['mainpart'] = $this->_make_request('add');
	}
	
	function display_item_edit() {
		$mainpart = $this->_make_request($this->feObj->operation);
		$id = $this->feObj->_request['id'];
		if( $id > 0 ) {
			$_valid = $this->modelObj->checkItem($id);
			if( $_valid == true ) {
				$item = $this->modelObj->getItem($id, 'id', -1);
				
				$content_wrap = array(
							'mainpart' => $mainpart,
							'item' => $item,
									);
			} else {
				$content_wrap = array(
							'mainpart' => $this->_make_request('no'),
								);
			}
		} else {
			$is_default = true;
		}
	}
	
	function display_item_delete() {
		
	}
	
	function display_items() {
		
	}
	
	function display_items_detail() {
		
	}
	
	function display_items_search() {
		
	}
	
	//CHECK
	function check_action_add($data_array) {
		return true;
	}
	
	function check_action_update($id, $data_array) {
		return true;
	}
	
	function check_action_delete($id) {
		return true;
	}
	
	function check_action_deletes($ids) {
		return true;
	}
	
	//ACTION
	//
	function do_action_default() {
		return false;
	}
	
	function do_action_add() {
		$this->vObj->check($_POST, array('name'));
		if( $this->vObj->_valid() ) {
			$data_array = $this->_doFilter($_POST, 'add');
			$this->modelObj->addItem($data_array);
			//
			$this->_redirect('', array('id','op','action'), 's');
		} else {
			$GLOBALS['_SUBMITTED_'] = 1;
			
			$vm = $this->vObj->_result(':error');
			$this->feObj->addInject('vm', $vm);
		}
	}
	
	function do_action_update() {
		$data_array = array();
		$id = trim(strtolower(util_get_param($_POST, 'id')));
		if( $id > 0 ) {
			$this->vObj->check($_POST, array('name'));
			if( $this->vObj->_valid() ) {
				$data_array = $this->_doFilter($_POST, 'update');
				$this->modelObj->updateItem($id, $data_array);
				//
				$this->_redirect('', array('id','op','action'), 's');
			} else {
				$GLOBALS['_SUBMITTED_'] = 1;
				
				$vm = $this->vObj->_result(':error');
				$this->feObj->addInject('vm', $vm);
			}
		} else {
			$this->_redirect('', array('id','op','action'), 's');
		}
	}
	
	function do_action_delete() {
		$id = trim(strtolower(util_get_param($_POST, 'id')));
		if( $id > 0 ) {
			$this->modelObj->deleteItem($id);
		}
		//
		$this->_redirect('', array('id','op','action'), 's');
	}
	
	function do_action_deletes() {
		$ids = util_get_param($_POST, 'cb');
		if( is_array($ids) ) {
			$this->modelObj->deleteItems($ids);
		}
		//
		$this->_redirect('', array('id','op','action'), 's');
	}
	
	function do_action_setstatusopen() {
		$ids = util_get_param($_POST, 'cb');
		if( is_array($ids) ) {
			$this->modelObj->setItemsStatus($ids, 1);
		}
		//
		$this->_redirect('', array('id','op','action'), 's');
	}
	
	function do_action_setstatusclose() {
		$ids = util_get_param($_POST, 'cb');
		if( is_array($ids) ) {
			$this->modelObj->setItemsStatus($ids, 0);
		}
		//
		$this->_redirect('', array('id','op','action'), 's');
	}
	
	function do_action_setfield() {
		$ids = util_get_param($_POST, 'cb');
		$field = util_get_param($_POST, 'field');
		if( is_array($ids) ) {
			$this->modelObj->updateItemsF($ids, $field);
		}
		//
		$this->_redirect('', array('id','op','action'), 's');
	}
}
//
?>